package mi.payment.transaction.sale;

import android.annotation.SuppressLint;

import lainey.ui.utils.LogUtil;
import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.Configs;
import mi.payment.controller.BaseController;
import mi.payment.db.dao.PayDetail;
import mi.payment.transaction.BaseQPSFreePwdAndSignController;
import mi.payment.ui.base.PageRoute;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.iso8583.QPSFreePwdAndSign;
import mi.payment.utils.transaction.DoubleFreeConsumptionTask;
import sunmi.payment.ui.dialog.LoadDialog;

public class DoubleFreeSaleController extends BaseController {

    private TradeData mTradeData;

    public DoubleFreeSaleController(ParentActivity activity, TradeData tradeData) {
        super(activity);
        this.mTradeData = tradeData;
    }

    public void operationProcess() {
        String strTag9F51 = "";
        String strTagDF71 = "";
        String dataTag4F = mTradeData.getDataTag4F();       // 非接卡获取到的AID数据
        String dataTag9F6C = mTradeData.getDataTag9F6C();   // 非接卡获取到的 9F6C卡片终端协商结果要求Pin
        LogUtil.e(Configs.TAG, "非接卡获取到的AID数据:" + dataTag4F);
        LogUtil.e(Configs.TAG, " 非接卡获取到的 9F6C卡片终端协商结果要求Pin:" + dataTag9F6C);
        boolean judge = dataTag4F == null || dataTag4F.substring(0, 14).equals("A0000003330101");
        if (judge) {
            strTag9F51 = "0156"; // 9F51——应用货币代码（如果返回0156，代表是内卡）
            strTagDF71 = "0156"; // DF71——电子现金终端支持指示器（如果返回0156，代表是内卡）
        }

        PayDetail payDetail = mTradeData.getPayDetail();
        BaseQPSFreePwdAndSignController controller = new QPSFreePwdAndSign();

        // 免签操作
        boolean freeSign = controller.isFreeSign(payDetail.amount);
        LogUtil.e(Configs.TAG, "freeSign:" + freeSign);
        if (freeSign) {
            payDetail.setIsFreeSign(true);
        }

        // 免密操作
        boolean freePWD = controller.isFreePassword(payDetail.CardNo, dataTag4F, payDetail.amount, dataTag9F6C, strTag9F51, strTagDF71);
        LogUtil.e(Configs.TAG, "freePWD:" + freePWD);
        if (freePWD) {
            payDetail.setIsFreePWD(true);
            startOnline();
        } else {
            // 跳转到输入密码流程
            ConsumeInputPWDActivity.startAction(mActivity);
            mActivity.finish();
        }
    }

    @SuppressLint("StaticFieldLeak")
    private void startOnline() {
        // 消费
        mTradeData.getPayDetail().setTransactionType(1);
        DoubleFreeConsumptionTask task = new DoubleFreeConsumptionTask(mActivity) {

            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                PageRoute.startAction(mActivity, PageRoute.TYPE_CONSUME, resPay);
            }

            @Override
            protected void onTaskTimeoutImpl() {
                PageRoute.startTimeoutAction(mActivity, PageRoute.TYPE_CONSUME, mTradeData.payDetail);
            }

        };
        LoadDialog dialog = mActivity.getLoadingDialog(R.string.load_free_password);
        task.setLoadDialog(dialog);
        task.execute(mTradeData);
    }


}
